package com.java.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class ShowFloatBit {

	public static void main(String[] args) {
		ByteArrayInputStream bis=null;
		ByteArrayOutputStream bos=null;
		DataOutputStream dos=null;
		DataInputStream dis=null;
		float[] data={12.2f,3.14f,3.21f,42.6f,52.4f,65.8f,75.3f};
		try {
			bos=new ByteArrayOutputStream(28);
			dos=new DataOutputStream(bos);
			for(int i=0;i<data.length;i++){
				dos.writeFloat(data[i]);
			}
			bis=new ByteArrayInputStream(bos.toByteArray());
			dis=new DataInputStream(bis);
//			showFloatBit(dis.readInt());
			int i=0;
			while(dis.available()!=0&&i<data.length){
				System.out.print(dis.readFloat()+"\tBit:");
				showFloatBit(Float.floatToIntBits(data[i]));
				i++;
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
//关闭流时，先关闭上层流，再关闭底层流
			if(dos!=null)try {dos.close();} catch (IOException e) {}
			if(bos!=null)try {bos.close();} catch (IOException e) {}
			if(dis!=null)try {dis.close();} catch (IOException e) {}
			if(bis!=null)try {bis.close();} catch (IOException e) {}
		}
		
		
	}
	public static void showFloatBit(int n){
		for(int i=32;i>=0;i--){
			if((n&(1<<i))!=0){
				System.out.print("1");
			}else{
				System.out.print("0");
			}
			if((32-i)%8==0){
				System.out.print("  ");
			}
		}
		System.out.println();
			
	}
	

}
